// This class provides the implementation of a GuiInterface object,
// used to interface between the GUI and the backend code
//
// Created by River on 11/4/11
// Edited by Christopher Bowlsby on 11/5/11
// Updated by Raquel Rhodes on 11/7/11, 11/20/11, 11/22/11

#include "GuiInterface.h"
#include "createplayer.h"
#include <iostream>
#include <string.h>
#include "Utility.h"
#include <algorithm>
#include <QtGui/QMessageBox>
#include <QtGui/QDialog>
#include <QtGui/QFrame>
#include "NewOrContinueBox.h"
#include "GetFile.h"


// constructor
GuiInterface::GuiInterface(Game *ptrToCurrentGame,int argc, char* argv[]) : Interface(ptrToCurrentGame) {
    this->argc = argc;
    this->argv = argv;
}//constructor


// this function sets up the player object for use in the game
void GuiInterface::setupPlayer() {

    string fileName;
    QApplication app(argc, argv);
    NewOrContinueBox newOrContinue(new QWidget());
    int result = newOrContinue.showBox();

    // if the user desires to start a new game
    if(result == 0)
    {   // create player
        createPlayer playerSetup;
        playerSetup.exec();

        // copy player stats into game pointer
        gamePtr->setupPlayer(playerSetup.getPlayerName());

        // will use this version later (game and a bunch of other header files
        //need to be modified first)
        //gamePtr->setupPlayer(playerSetup.getPlayerName(), playerSetup.getPlayerClass(),
                //playerSetup.getPlayerStats());
    }
    else{
        GetFile getFile;
        fileName = getFile.openFile();
        std::cout << fileName << std::endl;
        exit(0);
    }
}//setupPlayer


// after construction of the player object, the main interface for the
// game is loaded in this function
void GuiInterface::loadInterface() {

    QApplication app(argc, argv);
    w = new Dungeons_and_Princesses_gui;
    update("");

    /****need to load contents into view before show()  *****/
    QString text = this->showDescription();
    w->setTextDesc(text);

    w->show();
    app.exec();

    exit(0);
}


void GuiInterface::update(string errorMSG) {

    if (errorMSG != "") {
        //cout << endl << errorMSG << endl << endl;
    }
    QString text = this->showDescription(); //print text description
    w->getInterfaceInstance(this);
    w->setTextDesc(text);
  //  showDirections();
  //  showRoomInventory();

/*
    const vector<Listing> * roomInv = gamePtr->getCurrentRoom()->getManifest();

    if (roomInv->size() > 0) {
        //std::cout << "\n" << "In the room you see the following : " << endl;
        //for_each(roomInv->begin(), roomInv->end(), Utility::printListingOBject);

        //std::cout << endl;

    }*/
}



QString GuiInterface::showDescription()
{
    std::string text = gamePtr->getCurrentRoom()->Base::getDesc();
    QString roomDesc = QString::fromStdString(text);
    QString dir = this->showDirections();
    roomDesc.append("\n");
    roomDesc.append(dir);
    return roomDesc;
}//showDescription

/*void GuiInterface::showPrompt() {

    string command;
    //cout << endl << "------------------------------------" << endl << gamePtr->getCurrentPlayer()->getDesc() << ", what do you wish to do ?   ";
    //getline (cin,command);

    gamePtr->executeCommand(command);
} */

void GuiInterface::showPlayerInventory() {

    const vector<Listing> * playerInv = gamePtr->getCurrentPlayer()->inventory.getManifest();
    QString textInvTemp, textInv;
    textInv = "";
    textInvTemp = "";

    if (playerInv->size() > 0) {
        //std::cout << "\n" << "You have in your inventory the following items : " << endl << endl;
      //  for_each(playerInv->begin(), playerInv->end(), Utility::printListingOBject);
        //std::cout << endl;
     /*   for (int i = 0; playerInv->size(); playerInv++)
        {
            std::string inv = playerInv[i].data();
            textInvTemp = QString::fromStdString(inv);
            textInv.append(textInvTemp);
            textInv.append(", ");
        }*/
        update();
    }

    else {
        update("You do not have anything in your inventory");
    }
}

void GuiInterface::showHelp(vector<std::string> *helpFile) {

    vector<std::string>::iterator helpLine;

    //std::cout << endl << endl;
    for (helpLine = helpFile->begin(); helpLine != helpFile->end(); helpLine++) {
       // std::cout << *helpLine << endl;
    }
    update();
}

void GuiInterface::getResponse(std::string prompt, std::string &response) {


        //cout << prompt << flush;
        //getline(cin, response);

        Utility::removeGarbage(response);
        Utility::lowerCase(response);
}

QString GuiInterface::showDirections()
{
    QString roomDescTemp, roomDesc;
    std::string text = "";
    for (int directionNum = 0; directionNum < 4; directionNum++) {
        text = gamePtr->getCurrentRoom()->getDirections()[directionNum].getDesc();
        roomDescTemp = QString::fromStdString(text);
        roomDesc.append("\n");
        roomDesc.append(roomDescTemp);
    }//for
   return roomDesc;
}//showDirections

void GuiInterface::somethingHappened(std::string str)
{
    gamePtr->executeCommand(str);
    update();
}//somethingHappened

void GuiInterface::showRoomInventory() {

}
